// 获取我的账本，报销管理，搜索账单，分期·周期，设置元素
let selectList = document.querySelectorAll(".selectList li");
// 获取头像元素和下面的名字 以及整个用户信息框
let headFrame = document.querySelector(".headFrame img");
let useName = document.querySelector(".userInformation .userName");
//获取返回按钮
let headerGoBack = document.querySelector(".headerGoBack");

// 存储我的账本，报销管理，搜索账单，分期·周期，设置元素跳转链接
let selectListWebpage = [
  "../public/myAccountBook.html",
  "../public/reimbursementManagement.html",
  "../public/searchBill.html",
  "../public/settings.html"
];

// 点击按钮返回上一级
headerGoBack.addEventListener("click", function () {
  console.log("1");
  window.location.href = "./firstPage.html";
});
// 点击按钮跳转
selectList.forEach((item, index) => {
  item.addEventListener("click", function () {
    window.location.href = selectListWebpage[index];
  });
});
// 判断用户有没有登录
loginYesNo();
async function loginYesNo() {
  let token = window.localStorage.token;
  if (!token) {
    headFrame.src = "http://121.37.252.168:8080/api/user/getAvatar/default1.jpg";
    useName.textContent = "未登录";
    headFrame.onclick = function () {
      window.location.href = "../public/login.html";
    };
  } else {
    let res = await axios({
      method: "post",
      url: "http://121.37.252.168:8080/api/user/getUserInfo",
      headers: {
        Authorization: `Bearer ${token}`
      }
    });
    let data = res.data.data;
    headFrame.src = `http://121.37.252.168:8080/api/user/getAvatar/${data.avatar}`;
    useName.textContent = `${data.username}`;
    headFrame.onclick = function () {
      window.location.href = "../public/user.html";
    };
  }
}
